package cn.tedu.car.backend.checklist.service;

import cn.tedu.car.backend.checklist.mapper.CheckListMapper;

import cn.tedu.car.backend.checklist.pojo.dto.CheckInsertParam;
import cn.tedu.car.backend.checklist.pojo.dto.CheckListParam;
import cn.tedu.car.backend.checklist.pojo.entity.CheckList;
import cn.tedu.car.backend.checklist.pojo.vo.CheckListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.List;
import java.util.Random;

@Service
public class CheckListServiceImpl implements CheckListService {

    @Autowired
    private CheckListMapper checkListMapper;

    @Override
    public List<CheckListVO> selectList(CheckListParam checkListParam) {
       List<CheckListVO> list = checkListMapper.selectList(checkListParam);
       return list;
    }

    @Override
    public void deleteByNum(Long id) {
        checkListMapper.deleteByNum(id);
    }

    @Override
    public void updateCheckList(CheckList checkList) {
        CheckList updateData =new CheckList();
        updateData.setId(checkList.getId());
        BeanUtils.copyProperties(checkList,updateData);
        checkListMapper.updateCheckList(updateData);
    }

    @Override
    public void insertCheckList(CheckInsertParam checkList) {
/*        long longId = idGenerator.nextId();
//        int intId = (int) (longId & 0x7FFFFFFF); // 转换为int型，并防止溢出
        checkList.setId(longId);*/
        Long id = getOrderNo();
        checkList.setId(id);
        checkListMapper.insertCheckList(checkList);
    }

    private Long getOrderNo() {
//        long now = System.currentTimeMillis();
        LocalTime currentTime = LocalTime.now();
        int hour = currentTime.getHour();
        int minute = currentTime.getMinute();
        int second = currentTime.getSecond();
        long now = hour  + minute  + second;
        Random random = new Random();
        int r = random.nextInt(100);
        Long orderNo = r + now;
        return orderNo;
    }

}
